<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>CheckCCompilerFlag &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="CheckCompilerFlag" href="CheckCompilerFlag.html" />
    <link rel="prev" title="BundleUtilities" href="BundleUtilities.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="CheckCompilerFlag.html" title="CheckCompilerFlag"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="BundleUtilities.html" title="BundleUtilities"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CheckCCompilerFlag</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="checkccompilerflag">
<span id="module:CheckCCompilerFlag"></span><h1>CheckCCompilerFlag<a class="headerlink" href="#checkccompilerflag" title="Link to this heading">¶</a></h1>
<p>This module provides a command to check whether the C compiler supports a
given flag.</p>
<p>Load this module in a CMake project with:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CheckCCompilerFlag</span><span class="nf">)</span>
</pre></div>
</div>
<section id="commands">
<h2>Commands<a class="headerlink" href="#commands" title="Link to this heading">¶</a></h2>
<p>This module provides the following command:</p>
<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:check_c_compiler_flag">
<span class="sig-name descname"><span class="pre">check_c_compiler_flag</span></span><a class="headerlink" href="#command:check_c_compiler_flag" title="Link to this definition">¶</a></dt>
<dd><p>Checks once whether the C compiler supports a given flag:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">check_c_compiler_flag(</span><span class="nv">&lt;flag&gt;</span><span class="w"> </span><span class="nv">&lt;variable&gt;</span><span class="nf">)</span>
</pre></div>
</div>
<p>This command checks once that the <code class="docutils literal notranslate"><span class="pre">&lt;flag&gt;</span></code> is accepted by the C compiler
without producing a diagnostic message.  Multiple flags can be specified
in one argument as a string using a <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a>.</p>
<p>The result of the check is stored in the internal cache variable specified
by <code class="docutils literal notranslate"><span class="pre">&lt;variable&gt;</span></code>, with boolean true for success and boolean false for
failure.</p>
<p>A successful result only indicates that the compiler did not report an
error when given the flag.  Whether the flag has any effect, or the
intended one, is outside the scope of this module.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Since the underlying <span class="target" id="index-0-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> command also uses flags from
variables like <span class="target" id="index-0-variable:CMAKE_&lt;LANG&gt;_FLAGS"></span><a class="reference internal" href="../variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_&lt;LANG&gt;_FLAGS" title="CMAKE_&lt;LANG&gt;_FLAGS"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_&lt;LANG&gt;_FLAGS</span></code></a>, unknown or unsupported
flags in those variables may result in a false negative for this check.</p>
</div>
<p class="rubric">Variables Affecting the Check</p>
<p>The following variables may be set before calling this command to modify
the way the check is run:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_FLAGS</span></code></dt><dd><p>A space-separated string of additional flags to pass to the compiler.
A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> will not work.
The contents of <span class="target" id="index-1-variable:CMAKE_&lt;LANG&gt;_FLAGS"></span><a class="reference internal" href="../variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_&lt;LANG&gt;_FLAGS" title="CMAKE_&lt;LANG&gt;_FLAGS"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_&lt;LANG&gt;_FLAGS</span></code></a> and its associated
configuration-specific <span class="target" id="index-0-variable:CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;"></span><a class="reference internal" href="../variable/CMAKE_LANG_FLAGS_CONFIG.html#variable:CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;" title="CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;</span></code></a> variables
are automatically prepended to the compiler command before the contents of
this variable.</p>
</dd>
</dl>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_DEFINITIONS</span></code></dt><dd><p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of compiler
definitions, each of the form <code class="docutils literal notranslate"><span class="pre">-DFOO</span></code> or <code class="docutils literal notranslate"><span class="pre">-DFOO=bar</span></code>.  A definition for
the name specified by the result variable argument of the check
command is also added automatically.</p>
</dd>
</dl>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_INCLUDES</span></code></dt><dd><p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of header
search paths to pass to the compiler.  These will be the only header
search paths used; the contents of the <span class="target" id="index-0-prop_dir:INCLUDE_DIRECTORIES"></span><a class="reference internal" href="../prop_dir/INCLUDE_DIRECTORIES.html#prop_dir:INCLUDE_DIRECTORIES" title="INCLUDE_DIRECTORIES"><code class="xref cmake cmake-prop_dir docutils literal notranslate"><span class="pre">INCLUDE_DIRECTORIES</span></code></a>
directory property will be ignored.</p>
</dd>
</dl>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_LINK_OPTIONS</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.14.</span></p>
</div>
<p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of options to
add to the link command (see <span class="target" id="index-1-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> for further details).</p>
</dd>
</dl>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_LIBRARIES</span></code></dt><dd><p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of libraries to
add to the link command.  These can be the names of system libraries, or
they can be <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">Imported Targets</span></a> (see <span class="target" id="index-2-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> for further
details).</p>
</dd>
</dl>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_LINK_DIRECTORIES</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.31.</span></p>
</div>
<p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of library search
paths to pass to the linker (see <span class="target" id="index-3-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> for further
details).</p>
</dd>
</dl>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_QUIET</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.1.</span></p>
</div>
<p>If this variable evaluates to a boolean true value, all status messages
associated with the check will be suppressed.</p>
</dd>
</dl>
<dl class="simple">
<dt><span class="target" id="index-0-variable:CMAKE_TRY_COMPILE_TARGET_TYPE"></span><a class="reference internal" href="../variable/CMAKE_TRY_COMPILE_TARGET_TYPE.html#variable:CMAKE_TRY_COMPILE_TARGET_TYPE" title="CMAKE_TRY_COMPILE_TARGET_TYPE"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_TRY_COMPILE_TARGET_TYPE</span></code></a></dt><dd><p>Internally, the <span class="target" id="index-4-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> command is used to perform the
check, and this variable controls the type of target it creates.  If this
variable is set to <code class="docutils literal notranslate"><span class="pre">EXECUTABLE</span></code> (the default), the check compiles and
links the test source code as an executable program.  If set to
<code class="docutils literal notranslate"><span class="pre">STATIC_LIBRARY</span></code>, the test source code is compiled but not linked.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>The following example demonstrates how to use this module to check support
for the C compiler flag <code class="docutils literal notranslate"><span class="pre">-fno-optimize-strlen</span></code>, which disables
optimizations related to the <code class="docutils literal notranslate"><span class="pre">strlen()</span></code> C function in GCC and Clang
compilers.  The result of the check is stored in the internal cache
variable <code class="docutils literal notranslate"><span class="pre">HAVE_FNO_OPTIMIZE_STRLEN</span></code>, and the flag is conditionally enabled
using the <span class="target" id="index-0-command:target_compile_options"></span><a class="reference internal" href="../command/target_compile_options.html#command:target_compile_options" title="target_compile_options"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">target_compile_options()</span></code></a> command.  The
<span class="target" id="index-0-genex:COMPILE_LANGUAGE:languages"></span><a class="reference internal" href="../manual/cmake-generator-expressions.7.html#genex:COMPILE_LANGUAGE:languages" title="COMPILE_LANGUAGE:languages"><code class="xref cmake cmake-genex docutils literal notranslate"><span class="pre">$&lt;COMPILE_LANGUAGE:...&gt;</span></code></a> generator
expression ensures that the flag is added only to <code class="docutils literal notranslate"><span class="pre">C</span></code> source files.</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CheckCCompilerFlag</span><span class="nf">)</span>
<span class="nf">check_c_compiler_flag(</span><span class="p">-</span><span class="nb">fno-optimize-strlen</span><span class="w"> </span><span class="no">HAVE_FNO_OPTIMIZE_STRLEN</span><span class="nf">)</span>

<span class="nf">if(</span><span class="no">HAVE_FNO_OPTIMIZE_STRLEN</span><span class="nf">)</span>
<span class="w">  </span><span class="nf">target_compile_options(</span>
<span class="w">    </span><span class="nb">example</span>
<span class="w">    </span><span class="no">PRIVATE</span><span class="w"> </span><span class="o">$&lt;$&lt;</span><span class="no">COMPILE_LANGUAGE</span><span class="o">:</span><span class="no">C</span><span class="o">&gt;:</span><span class="p">-</span><span class="nb">fno-optimize-strlen</span><span class="o">&gt;</span>
<span class="w">  </span><span class="nf">)</span>
<span class="nf">endif()</span>
</pre></div>
</div>
</section>
<section id="see-also">
<h2>See Also<a class="headerlink" href="#see-also" title="Link to this heading">¶</a></h2>
<ul class="simple">
<li><p>The <span class="target" id="index-0-module:CheckCompilerFlag"></span><a class="reference internal" href="CheckCompilerFlag.html#module:CheckCompilerFlag" title="CheckCompilerFlag"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">CheckCompilerFlag</span></code></a> module for a more general command to check
whether a compiler flag is supported.</p></li>
</ul>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">CheckCCompilerFlag</a><ul>
<li><a class="reference internal" href="#commands">Commands</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#see-also">See Also</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="BundleUtilities.html"
                          title="previous chapter">BundleUtilities</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="CheckCompilerFlag.html"
                          title="next chapter">CheckCompilerFlag</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/CheckCCompilerFlag.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="CheckCompilerFlag.html" title="CheckCompilerFlag"
             >next</a> |</li>
        <li class="right" >
          <a href="BundleUtilities.html" title="BundleUtilities"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CheckCCompilerFlag</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>